#include "control.h"
#include <string.h>
//#include <stdio.h>
#include <stdlib.h>
#include <avr/eeprom.h>

//volatile uint8_t rxbuffer[RXBUFFSIZE];
//volatile uint8_t rxi = 0;
//volatile uint8_t rxbuffovfl = 0;


//bool next;
//bool inputmode;
//bool inputmode_donly;

typedef struct{
//	volatile uint8_t rxi;
	uint8_t digitbuffidx;
	int8_t digitbuff[3];
	uint8_t ionamebuffidx;
//	char ionames[IOPORTS][IONAMEMAXLENGTH + 1];

}globs_t;

globs_t globals;


//int8_t inputmodecount;
//int8_t ionamebuff[IONAMEMAXLENGTH + 1]; 
//uint8_t ionamebuffidx;

//int8_t digitbuff[2 + 1];
//uint8_t digitbuffidx;
 
char ionames[IOPORTS][IONAMEMAXLENGTH + 1] EEMEM = {"PC1", "PC2", "PC3", "PC4", "PC5", "PC6", "PC7", "PC8"};


/*****************************************************
 * process input from rxbuffer
 *****************************************************/
void getsequence(void)
{
	if(rxi > 0)
	{
		uint8_t val;
		uint8_t i = 0;
		val = rxbuffer[i];
		controlglobals_t *conglobs = &controlglobs;
		
		if(!(val == 27))
		{
			if(val == 13)	//Enter	
			{
				menu *submenu = (menu*) pgm_read_word(&mymenu[selectedpos].sub);
				void (*submfunc)();
				submfunc = (PGM_VOID_P) pgm_read_word(&submenu[subselectedpos].fp);

				if((submenu != NULL) && (submfunc != NULL))
				{	
					submfunc();
				}
			}
			else if(conglobs->inputmode)
			{	
				globs_t *globs = &globals;

				if(conglobs->inputmode_donly)
				{
					if((val >= 48) && (val <= 58) && (globs->digitbuffidx < 2))
					{
						echo(val);
						globs->digitbuff[globs->digitbuffidx] = val;
						globs->digitbuffidx++;
					}
				}
				else if(globs->ionamebuffidx < IONAMEMAXLENGTH)
				{
					echo(val);
					ionamebuff[globs->ionamebuffidx] = val;
					globs->ionamebuffidx++;
				}					
			}			
			clearsequence(i);
		}
	
		if(rxi > 2)
		{
			i++;
			val = rxbuffer[i];
				
			if(val != '[')
			{
				clearsequence(i);
			}
			else	
			{
				i++;
				val = rxbuffer[i];
				
				if(val == 'A')		//UP
				{					
					setsubm(false);
				}
				if(val == 'B')		//DOWN
				{
					setsubm(true);
				}

				if(!conglobs->inputmode)
				{
					if(val == 'C')		//RIGHT
					{
						setmainm(true);

					}
					if(val == 'D')		//LEFT
					{
						setmainm(false);
					}
				}
				clearsequence(i);
			}
		}
	}
}


void getdigitinput()
{
	controlglobals_t *conglobs = &controlglobs;
	conglobs->inputmode ^= 1;
	conglobs->inputmode_donly ^= 1;
	reprint ^= 1;
	globs_t *globs = &globals;
	
	if(conglobs->inputmode)
	{
		globs->digitbuffidx = 0;
		strcpy((char*)globs->digitbuff, "  ");
		setmode(MODE_REVERSED);
	}
	else
	{
		setmode(MODE_NONE);
	}
}


/*****************************************************
 * clear processed or bad input from buffer
 *****************************************************/
void clearsequence(uint8_t slength)
{
	cli();
	uint8_t i = 0;

	while((i <= rxi) &&(i <= (RXBUFFSIZE -(slength + 1))))
	{
		rxbuffer[i] = rxbuffer[i + slength + 1];
		i++;
	}	
	
	rxi -= (slength + 1);

	sei();
}

void setio()
{
	setoutp(subselectedpos);
}

/*****************************************************
 * save time config from inputfield
 *****************************************************/
void settime()
{
	getdigitinput();
	globs_t *globs = &globals;
	controlglobals_t *conglobs = &controlglobs;

	if(conglobs->inputmode)
	{
		print_inputfield();
	}
	else
	{
		if(subselectedpos == 10)
		{	
			uint8_t minutes_temp = atoi((char*)globs->digitbuff);
			if(minutes_temp < 60)
			{
				minutes = minutes_temp;
				seconds = 0;
			}
		}
		if(subselectedpos == 11)
		{
			uint8_t	hours_temp = atoi((char*)globs->digitbuff);
			if(hours_temp < 24)
			{
				hours = hours_temp;
				seconds = 0;
			}
		}
	}
	globs->digitbuffidx = 0;
	p_menu();
}

/*****************************************************
 * save dalay-value from inputfield
 *****************************************************/
void setonoffdel()
{
	getdigitinput();
	globs_t *globs = &globals;
	controlglobals_t *conglobs = &controlglobs;

	if(conglobs->inputmode)
	{
		print_inputfield();
	}
	else
	{
		if(subselectedpos == (IOPORTS))
		{	
			*delay[0] = atoi((char*)globs->digitbuff);
		}
		if(subselectedpos == (IOPORTS + 1))
		{
			*delay[1] = atoi((char*)globs->digitbuff);
		}
	}
	globs->digitbuffidx = 0;
	p_menu();
}


menu *mmenu;

void setmainm(bool next)
{
	if(next)
	{
		selectedpos = pgm_read_word(&mymenu[selectedpos].next);
	}
	else
	{
		selectedpos = pgm_read_word(&mymenu[selectedpos].prev);
	}
	
	p_menu();
}

/*****************************************************
 * change subselectedpos and reprint 
 *****************************************************/
void setsubm(bool next)
{

	menu *submenu = (menu*) pgm_read_word(&mymenu[selectedpos].sub);

	if(next)
	{	
		subselectedpos = pgm_read_byte(&submenu[subselectedpos].next);
		
	}
	else
	{
		subselectedpos = pgm_read_byte(&submenu[subselectedpos].prev);		
	}

	p_menu();
}


/*****************************************************
 * change ionames (max 8 characters)
 *****************************************************/
void setconfig()
{
	controlglobals_t *conglobs = &controlglobs;
	conglobs->inputmode ^= 1;
	reprint ^= 1;
    globs_t *globs = &globals;

	if(conglobs->inputmode)
	{
		globs->ionamebuffidx = 0;
		strcpy((char*)ionamebuff, "        ");
		print_inputfield();
	}
	else
	{
		setmode(MODE_NONE);
		ionamebuff[globs->ionamebuffidx] = '\0';
		eeprom_write_block(ionamebuff, ionames[subselectedpos], sizeof(ionamebuff));
		p_menu();
	}
	globs->ionamebuffidx = 0;
}

